﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace OZDoo.Core.Caching
{
    /// <summary>
    /// 缓存功能提供者,采用web缓存的方式实现缓存功能
    /// </summary>
    class WebCacheProvider : CacheProvider, ICache
    {
        //>> Based on Factor = 5 default value
        public readonly int DayFactor = 17280;
        public readonly int HourFactor = 720;
        public readonly int MinuteFactor = 12;
        public readonly double SecondFactor = 0.2;
        /// <summary>
        /// 创建缓存对象
        /// </summary>
        private readonly System.Web.Caching.Cache _cache;
        /// <summary>
        /// 缓存时间因子
        /// </summary>
        private int Factor = 5;
        /// <summary>
        /// 从新设定缓存时间因子
        /// </summary>
        /// <param name="cacheFactor">整型,缓存时间因子</param>
        public override void ReSetFactor(int cacheFactor)
        {
            Factor = cacheFactor;
        }

        /// <summary>
        /// 构造类
        /// </summary>
        public WebCacheProvider()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }
        /// <summary>
        /// 提供够初始化接口实现
        /// </summary>
        /// <param name="name">提供者名称,字符串</param>
        /// <param name="config">提供者初始化的配置,NameValueCollection键值对</param>
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);

        }

        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public override void Clear()
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }

            foreach (string key in al)
            {
                _cache.Remove(key);
            }

        }
        /// <summary>
        /// 从缓存删除指定模式的项目
        /// </summary>
        /// <param name="pattern">所包含的字符串</param>
        public override void RemoveByPattern(string pattern)
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            while (CacheEnum.MoveNext())
            {
                if (regex.IsMatch(CacheEnum.Key.ToString()))
                    _cache.Remove(CacheEnum.Key.ToString());
            }
        }

        /// <summary>
        /// 从缓存删除指定项目
        /// </summary>
        /// <param name="key">要删除缓存内容的唯一标识</param>
        public override void Remove(string key)
        {
            _cache.Remove(key);
        }

        /// <summary>
        /// 插入一个对象到缓存中
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        public override void Insert(string key, object obj)
        {
            Insert(key, obj, null, 1);
        }
        /// <summary>
        /// 插入一个对象到缓存中
        ///  </summary> 
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="dep">依附性关系</param>
        public override void Insert(string key, object obj, CacheDependency dep)
        {
            Insert(key, obj, dep, MinuteFactor * 3);
        }
        /// <summary>
        /// 插入一个对象到缓存中
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="seconds">要存储的时间,秒</param>
        public override void Insert(string key, object obj, int seconds)
        {
            Insert(key, obj, null, seconds);
        }
        /// <summary>
        /// 插入一个对象到缓存中
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="seconds">要存储的时间,秒</param>
        /// <param name="priority">相对优先级</param>
        public override void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            Insert(key, obj, null, seconds, priority);
        }
        /// <summary>
        /// 插入一个对象到缓存中
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="dep">依附性关系</param>
        /// <param name="seconds">要存储的时间,秒</param>
        public override void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }
        /// <summary>
        /// 插入一个对象到缓存中
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="dep">依附性关系</param>
        /// <param name="seconds">要存储的时间,秒</param>
        /// <param name="priority">相对优先级</param>
        public override void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
            }

        }
        /// <summary>
        /// 小限度缓存一个内容
        /// </summary>
        /// <param name="key">缓存内容的唯一标识</param>
        /// <param name="obj">要缓存的内容</param>
        /// <param name="secondFactor">秒时间因子,与默认时间因子相称后就是最小限度缓存的时间</param>
        public override void MicroInsert(string key, object obj, int secondFactor)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
            }
        }

        /// <summary>
        /// 最大限度缓存内容
        /// </summary>
        /// <param name="key">标识,要添加缓存内容的唯一标识</param>
        /// <param name="value">要添加的缓存内容</param>
        public override void Max(string key, object obj)
        {
            Max(key, obj, null);
        }
        /// <summary>
        /// 最大限度缓存内容
        /// </summary>
        /// <param name="key">标识,要添加缓存内容的唯一标识<</param>
        /// <param name="obj">要添加的缓存内容</param>
        /// <param name="dep">依附性关系</param>
        public override void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }
        /// <summary>
        /// 插入常驻缓存内容
        /// </summary>
        /// <param name="key">标识,要添加缓存内容的唯一标识</param>
        /// <param name="obj">要添加的缓存内容</param>
        public override void Permanent(string key, object obj)
        {
            Permanent(key, obj, null);
        }
        /// <summary>
        /// 插入常驻缓存内容
        /// </summary>
        /// <param name="key">标识,要添加缓存内容的唯一标识</param>
        /// <param name="obj">要添加的缓存内容</param>
        /// <param name="dep">依附性关系</param>
        public override void Permanent(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
            }
        }
        /// <summary>
        /// 获取缓存的内容
        /// </summary>
        /// <param name="key">所要获取内容的唯一标识</param>
        /// <returns>返回缓存的内容,如果没有,返回null值</returns>
        public override object Get(string key)
        {
            return _cache[key];
        }

        /// <summary>
        /// 返回第二个因素计算值
        /// </summary>
        /// <param name="seconds">要计算的秒数</param>
        /// <returns>返回第二个因素计算值</returns>
        public override int SecondFactorCalculate(int seconds)
        {
            // Insert method below takes integer seconds, so we have to round any fractional values
            return Convert.ToInt32(Math.Round((double)seconds * SecondFactor));
        }



        public override string[] GetAllCache()
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
            while (CacheEnum.MoveNext())
            {
                list.Add((string)CacheEnum.Key);
            }
            return list.ToArray();
        }
    }
}
